package ca.concordia.soen6461.wichf.navigation.action;

import ca.concordia.soen6461.wichf.controller.Action;
import ca.concordia.soen6461.wichf.controller.Event;
import ca.concordia.soen6461.wichf.dao.PatientCartDao;
import ca.concordia.soen6461.wichf.entity.PatientCart;
import ca.concordia.soen6461.wichf.entity.ScheduledEvent;
import ca.concordia.soen6461.wichf.entity.User;
import ca.concordia.soen6461.wichf.navigation.event.PayEvent;

public class DoProcessPayment implements Action {

	private PatientCartDao patientCartDao;

	public DoProcessPayment(PatientCartDao patientCartDao) {
		this.patientCartDao = patientCartDao;
	}

	@Override
	public void perform(Event event) {
		PayEvent payEvent = (PayEvent) event;
		User user = payEvent.getSession().get(User.class);
		PatientCart patientCart = patientCartDao.get(user);
		PatientCart payCart = new PatientCart();
		for (int item : payEvent.getSelectedAppointments()) {
			ScheduledEvent appointment = patientCart.getAppointments().get(item);
			payCart.getAppointments().add(appointment);
			appointment.setConfirmed(true);
		}
		for (ScheduledEvent appointment : payCart.getAppointments()) {
			patientCart.getAppointments().remove(appointment);
		}
		event.keep(payCart);
	}

}
